#!/usr/bin/env python
# --*-- conding:utf-8 --*--
# @Time : 2024/1/25
# @Author : liangpp
# @Email : mobaicloud@163.com
# @Software : PyCharm


from carlargescreen.utils.get_alldata import get_alldata


def get_centerdata():
    allcars = list(get_alldata())
    # 汽车总数据
    sum_car = len(allcars)
    # 销量最高汽车
    highvolume_car = allcars[0].car_name
    # 单车最高销售量
    highcount = allcars[0].count
    # 销售最多车型
    model_car = {}
    max_model = 0
    most_model = ""
    for c in allcars:
        if model_car.get(c.model, None) == None:
            model_car[c.model] = 1
        else:
            model_car[c.model] += 1
    for k, v in model_car.items():
        if v > max_model:
            max_model = v
            most_model = k
    # 车型最多品牌
    brand_car = {}
    max_brand = 0
    most_brand = ""
    for c in allcars:
        if brand_car.get(c.brand_name, None) == None:
            brand_car[c.brand_name] = 1
        else:
            brand_car[c.brand_name] += 1
    for k, v in brand_car.items():
        if v > max_brand:
            max_brand = v
            most_brand = k
    # 车辆平均价格
    all_avg_price = []
    for c in allcars:
        list_price = c.price.split("-")
        c_avg_price = round((float(list_price[0]) + float(list_price[1])) / 2, 2)
        all_avg_price.append(c_avg_price)
    avg_price = round(sum(all_avg_price) / len(all_avg_price), 2)

    # print(sum_car, highvolume_car, highcount, most_model, most_brand, avg_price)
    return sum_car, highvolume_car, highcount, most_model, most_brand, avg_price


def get_rolldata():
    allcars = list(get_alldata())
    brand_car = {}
    for c in allcars:
        if brand_car.get(c.brand_name, None) == None:
            brand_car[c.brand_name] = 1
        else:
            brand_car[c.brand_name] += 1
    # print(brand_car)
    # lambda实现排序 TOP10
    sort_list = sorted(brand_car.items(), key=lambda d: d[1], reverse=True)[:10]
    rolldata_list = []
    for tup in sort_list:
        last_dict = {}
        last_dict["name"] = tup[0]
        last_dict["value"] = tup[1]
        rolldata_list.append(last_dict)
    # print(rolldata_list)
    return rolldata_list


def get_ratedata():
    allcars = list(get_alldata())
    rate_dict = {}
    for c in allcars:
        if rate_dict.get(c.energyType, None) == None:
            rate_dict[c.energyType] = 1
        else:
            rate_dict[c.energyType] += 1
    qiyou_rate = round(rate_dict.get("汽油") / len(allcars) * 100, 2)
    chundian_rate = round(rate_dict.get("纯电动") / len(allcars) * 100, 2)
    other_rate = round((len(allcars) - rate_dict.get("汽油") - rate_dict.get("纯电动")) / len(allcars) * 100, 2)
    # print(qiyou_rate, chundian_rate, other_rate)
    return qiyou_rate, chundian_rate, other_rate